package com.kingbull.webservice.shunt;

import com.kingbull.web.communic.Body;
import com.kingbull.web.communic.Head;
import com.kingbull.web.communic.Subject;
import com.kingbull.web.mapper.Mapper;
import com.kingbull.web.utils.ResponseCode;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

//业务入口文件
@Component
public class Entrance {
    @Autowired
    private Mapper mapper;

    @Autowired
    private BulkCharging bulkCharging;
    @Autowired
    private IDCardOrder idCardOrder;
    @Autowired
    private SmallTicket smallTicket;
    @Autowired
    private Weigh weigh;

    public String Main(Subject sub){
        Head head= sub.getHead();
        Body body= sub.getBody();
        BaseService base = null;
        switch(head.getServCode()){
            //过磅
            case "1001":
                base= weigh;
                break;
            //打印小票
            case "1003":
                base = smallTicket;
                break;
            //散装放料验证
            case "1004":
                base = bulkCharging;
                break;
//            //身份证取订单
//            case "1005":
//                base = idCardOrder;
//                break;
            default:
                sub.getHead().setCode(ResponseCode.ERROR_CODE);
                JSONObject object = JSONObject.fromObject(sub);
                return object.toString();
        }
        String back ="";
        try {
            Map<String,Object> map= mapper.getOrder(body.getIdCardNo(),head.getFirmID());
            Integer org= Integer.valueOf(map.get("orderType").toString());//订单类型（1 销售订单、2 原材料订单、3 倒料订单、4 其他材料订单）
            Integer ycl= Integer.valueOf(map.get("state").toString());//进厂状态（0默认 1进厂）
            if(org == 1){
                back= base.sale(sub);
            }else if (org == 2){
                back= base.rawMaterial(sub);
            }else if (org == 3){
                back= base.reverseOrder(sub);
            }else if (org == 4){
                back= base.otherMaterials(sub);
            }else{
                back= error(sub);
            }
        }catch (Exception e){
            System.err.println(e.toString());
            mapper.saveLog(error(sub),"1");
            return error(sub);
        }
        mapper.saveLog(error(sub),"1");
        return back;
    }

    String error(Subject sub){
        sub.getHead().setLedMsg("该厂不存在此人过磅订单");
        sub.getHead().setVoiceMsg("该厂不存在此人过磅订单");
        sub.getHead().setCode(ResponseCode.ERROR_CODE);
        JSONObject object = JSONObject.fromObject(sub);
        return object.toString();
    }

    public String IdCard(Subject sub){
        return idCardOrder.run(sub);
    }


}
